package leetcode.editor.cn;

import java.util.*;

/**
 * @author: yangwk
 * @date: 2023/2/7
 * @description:
 **/
public class P1604AlertUsingSameKeyCard {
    public static List<String> alertNames(String[] keyName, String[] keyTime) {
        Map<String, List<Integer>> map = new HashMap<>();
        List<String> result = new ArrayList<>();
        for (int i = 0; i < keyName.length; i++) {
            String name = keyName[i];
            final String time = keyTime[i];
            map.compute(name, (s, times) -> {
                if (times == null) {
                    times = new ArrayList<>();
                }
                String[] split = time.split(":");
                int minute = Integer.parseInt(split[0]) * 60 + Integer.parseInt(split[1]);
                times.add(minute);
                Collections.sort(times);
                if (times.size() >= 3) {
                    for (int j = 2; j < times.size(); j++) {
                        Integer s1 = times.get(j - 2);
                        Integer s2 = times.get(j);
                        if (s2 - s1 <= 60) {
                            result.add(name);
                            break;
                        }
                    }
                }
                return times;
            });
        }
        Collections.sort(result, String::compareTo);
        return result;
    }

    public static void main(String[] args) {
        String[] keyName = new String[]{"b", "b", "b", "b", "b", "b"};
        String[] keyTime = new String[]{"00:52", "10:59", "17:16", "00:36", "01:26", "22:42"};
        System.out.println(alertNames(keyName, keyTime));
    }
}
